


class WalkCommand extends command.Command {

    private x: number;
    private y: number;

    constructor(x: number, y: number) {
        super();
        this.x = x;
        this.y = y;
    }

    execute(notifyFinish: Function) {

        console.log(`开始走路！！！从${this.x},${this.y}出发`);
        const map = mapManager.getMap();
        map.grid.setEndNode(this.x, this.y);
        const findpath = new astar.AStar();
        findpath.setHeurisitic(findpath.diagonal)
        findpath.findPath(map.grid);
        const path = findpath._path;

        if (path) {

            //为啥要shift()？？？不删感觉也没问题？？？
            //path.shift();


            user.goto(path);


            const func = () => {
                user.removeEventListener("goOver", func);
                notifyFinish();
            }
            user.addEventListener("goOver", func)

            console.log("goOver");

        }
        else {
            console.log(" notifyFinish");
            notifyFinish();


        }
    }
}

class PickCommand extends command.Command {


    private weapon: Item;
    constructor(weapon: Item) {
        super();
        this.weapon = weapon;
    }

    execute(notifyFinish: Function) {
        user.pick(this.weapon)
        //  user.level++;
        const map = mapManager.getMap();
        this.weapon.view.alpha = 0;
        user.packagedItems.push(this.weapon);
        //为什么就是消失不了？！！！

        map.removeChild(this.weapon.view);
        map.removeItem(this.weapon);
        missionManager.update();
        notifyFinish();
    }
}

class GotoNextMapCommand extends command.Command {

    constructor(private mapId: string) {
        super();
    }

    execute() {

        console.log('换地图！！')
        mapManager.changeMap(this.mapId)
    }
}

class TalkCommand extends command.Command {

    private npc: Npc;
    constructor(npc: Npc) {
        super();
        this.npc = npc;
    }


    execute(notifyFinish: Function) {

        console.log(`开始对话:${this.npc.toString()}`)
        let mission: Mission | null = null;
        if (this.npc.canSubmitMissions.length > 0) {
            mission = this.npc.canSubmitMissions[0];
        }
        if (this.npc.canAcceptMissions.length > 0) {
            mission = this.npc.canAcceptMissions[0];
        }
        else {

        }
        if (mission) {
            const dialog = new TalkDialog(this.npc);
            dialogContainer.addChild(dialog);
            dialog.setMission(mission);
            dialog.addEventListener('close', () => {
                dialogContainer.removeChild(dialog)
                if (mission) {
                    if (mission.status == MissionStatus.CAN_SUBMIT) {
                        console.log('提交任务', mission.title)
                        missionManager.submitMission(mission);
                    }
                    else if (mission.status == MissionStatus.CAN_ACCEPT) {
                        console.log('接受任务', mission.title)
                        missionManager.acceptMission(mission);
                    }
                }
                notifyFinish();

            })
        }
        else {
            const dialog = new TalkDialog(this.npc);
            dialogContainer.addChild(dialog);
            const sentence = new renderer.TextField(100, 100);
            sentence.x = dialog.x + 200;
            sentence.y = dialog.y + 300;
            sentence.text = this.npc.text;
            stage.addChild(dialog);
            stage.addChild(sentence);


            window.onclick = (eventData: any) => {
                stage.removeChild(sentence);
                stage.removeChild(dialog);
                dialogContainer.removeChild(dialog)
            }
        }
        notifyFinish();
    }


}

